<?php
/*
 * Copyright (C) 2004-2011 wortzwei GmbH <info@wortzwei.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @since 14.01.2011
 * @author Dirk Dittmar
 */

/**
 * Solr-Search-Plugin
 * @package MantisPlugin
 * @subpackage MantisPlugin
 * @link http://www.wortzwei.de
 * @author Dirk Dittmar
 */

require_once( config_get( 'class_path' ) . 'MantisPlugin.class.php' );

require_once( dirname(__FILE__) . '/inc/SolrSearch_SolrService.class.php' );
require_once( dirname(__FILE__) . '/inc/SolrSearch_StringUtils.class.php' );

/**
 * SolrSearchPlugin Class
 */
class SolrSearchPlugin extends MantisPlugin {

	const SOLR_HOST  = 'solr_host';
	const SOLR_PORT  = 'solr_port';
	const SOLR_PATH  = 'solr_location';
	const SOLR_SCORE = 'solr_score';

	/**
	 * (non-PHPdoc)
	 * @see MantisPlugin::register()
	 */
	function register() {
		$this->name = 'Solr-Search-Plugin';
		$this->description = 'full text search with solr!';

		$this->version = '1.0.1';
		$this->requires = array(
			'MantisCore' => '1.2.0',
		);

		$this->page = 'config';

		$this->author = 'wortzwei GmbH';
		$this->contact = 'info@wortzwei.de';
		$this->url = 'http://www.wortzwei.de';
	}

	/**
	 * (non-PHPdoc)
	 * @see MantisPlugin::config()
	 */
	function config() {
		$config = array(
			self::SOLR_HOST  => 'localhost',
			self::SOLR_PORT  => '8080',
			self::SOLR_PATH  => '/solr',
			self::SOLR_SCORE => true
		);
		return $config;
	}

	/**
	 * (non-PHPdoc)
	 * @see MantisPlugin::hooks()
	 */
	function hooks() {
		$hooks = array(
			'EVENT_MENU_MAIN'       => 'solr_search_menu',
			'EVENT_REPORT_BUG'      => 'solr_insert_bug',
			'EVENT_UPDATE_BUG'      => 'solr_update_bug',
			'EVENT_BUG_DELETED'     => 'solr_delete_bug',
			'EVENT_BUG_ACTION'      => 'solr_update_bugs',
			'EVENT_BUGNOTE_ADD'     => 'solr_add_bugnote',
			'EVENT_BUGNOTE_EDIT'    => 'solr_edit_bugnote',
			'EVENT_BUGNOTE_DELETED' => 'solr_del_bugnote',
		);
		return $hooks;
	}

	function solr_search_menu($p_event) {
		return array( '<a href="' . plugin_page( 'search' ) . '">' . plugin_lang_get( 'search' ) . '</a>', );
	}

	function solr_add_bugnote($p_event, $bug_id, $bugnote_id) {
		$bug_data = bug_get($bug_id, true);
		self::addBug($bug_data);
	}

	function solr_edit_bugnote($p_event, $bug_id, $bugnote_id) {
		$bug_data = bug_get($bug_id, true);
		self::addBug($bug_data);
	}

	function solr_del_bugnote($p_event, $bug_id, $bugnote_id) {
		$bug_data = bug_get($bug_id, true);
		self::addBug($bug_data);
	}

	function solr_delete_bug($p_event, $bug_id) {
		$solr = new SolrSearch_SolrService(
					plugin_config_get(self::SOLR_HOST),
					plugin_config_get(self::SOLR_PORT),
					plugin_config_get(self::SOLR_PATH)
				);
		try {
			$solr->deleteById($bug_id);
			$solr->commit();
		} catch (Exception $e) {
			self::handleSolrException($e);
		}
	}

	function solr_insert_bug($p_event, $bug_data, $bug_id) {
		self::addBug($bug_data);
	}

	function solr_update_bug($p_event, $bug_data, $bug_id) {
		self::addBug($bug_data);
		return $bug_data;
	}

	function solr_update_bugs($p_event, $action, $bug_ids) {
		foreach ($bug_ids as $bug_id) {
			$bug_data = bug_get($bug_id, true);
			self::addBug($bug_data);
		}
	}

	private static function addBug(BugData $bug_data) {
		$solr = new SolrSearch_SolrService(
					plugin_config_get(self::SOLR_HOST),
					plugin_config_get(self::SOLR_PORT),
					plugin_config_get(self::SOLR_PATH)
				);
		try {
			$solr->addBug($bug_data);
			$solr->commit();
		} catch (Exception $e) {
			self::handleSolrException($e);
		}
	}

	private static function handleSolrException(Exception $e) {
		// mit project_get_all_user_rows() alle Benutzer holen und dann checken welcher admin ist?
		$allUser = project_get_all_user_rows();
		foreach ($allUser as $user) {
			$user_id = $user['id'];
			if (user_is_administrator($user_id) && user_is_enabled($user_id)) {
				$email = user_get_email($user_id);
				if (SolrSearch_StringUtils::isNotBlank($email)) {
					log_event( LOG_EMAIL, sprintf( 'SolrException = \'%s\', Email = \'%s\'.', $e->getMessage(), $email ) );
					$emailData = new EmailData();
					$emailData->email = $email;
					$emailData->subject = sprintf( 'SolrException: \'%s\'', $e->getMessage() );
					$emailData->body = plugin_lang_get('solr_exception_email');
					email_send($emailData);
				}
			}
		}
	}

}
